<?php
/**
 * Created by PhpStorm.
 * User: xpz
 * Date: 2023/8/23
 * Time: 10:38
 */

namespace app\api\command\tj;

use app\api\command\tj\TjBase;
use think\console\input\Option;
use think\console\Input;
use think\console\Output;
use think\Exception;
use think\config;
use think\Db;
class CreateSitemap extends TjBase
{
    protected function configure()
    {
        $this->setName('createsitemap')              		//定义命令的名字
        ->setDescription('createsitemap')      //定义命令的描述
        ->addArgument('method')                   //参数1
        ->addArgument('id');                    //文章id
    }


    //    php think createsitemap article
    protected function execute(Input $input, Output $output)
    {

        ini_set('max_execution_time', 600);//秒为单位，自己根据需要定义
        $method = $input->getArgument('method');
        if($method == 'article'){
            $this->article();
        } else if($method == 'sitemap'){
            $this->sitemap();
        }
    }

    private function article(){
        //已经生成1000个
        $poetryModel = model('app\common\model\Article');
        $poetryRs = $poetryModel->field('id,category_id,category_pid')->where(['status'=>1,'is_type '=>2])->order("id desc")->limit(0,50000)->select();
//        print_r($poetryRs);
//        exit;
        $sitemap ="";
        $siteTxt = "";
        if(!empty($poetryRs)){
            /*            $sitemap .= "<?xml version='1.0' encoding='UTF-8'?>";*/
//            $sitemap .="<sitemapindex>";
            foreach($poetryRs as $k=>$v){
//                $sitemap .="<sitemap>";
//                $sitemap .="<loc>".$v['url']."</loc>";
//                $sitemap .="<lastmod>".date("Y-m-d",time())."</lastmod>";
//                $sitemap .="</sitemap>";
                $siteTxt .=$v['url']."\r\n";
//                echo "$siteTxt";exit;
            }
//            $sitemap .="</sitemapindex>";
//            echo $sitemap;
        }
//        print_r($sitemap);
        if(!empty($siteTxt)){
            $filename = $this->rootPath."sitemap.txt";
//            $xmlRs = file_put_contents($this->createHtmlRootDir."sitemap.xml",$sitemap);
            $xmlRs = file_put_contents($filename,$siteTxt);
            if($xmlRs != false){
                echo "sitemap文件生成，路径:".$filename;
            }
        }
    }


    private function sitemap(){
        $articleModel = model('app\common\model\Article');
        $articleRs = $articleModel->field('id,category_id,createtime,category_pid,url_path,updatetime')->where(['status'=>1])->order("id desc")->select();
        $sitemap = '';
        if(!empty($articleRs)){
            foreach($articleRs as $k=>$v){
//                print_r($v);exit;
                $sitemap .='<url>';
                $sitemap .='<loc>'.$v['url'].'</loc>';
                $sitemap .='<lastmod>'.substr($v['ori_updatetime'],0,10).'</lastmod>';
                $sitemap .='<changefreq>daily</changefreq>';
                $sitemap .='<priority>0.7</priority>';
                $sitemap .='</url>';
            }
        }

        if(!empty($sitemap)){
            $this->create_sitemap_gg($sitemap,'sitemap');
        }

    }


    /**
     * 首页和列表页
     * @return array
     */
    private function sitemap_list(){
        $sitemap = '';
        $sitemap_tt = '';
        $sitemap_tt_m = '';
        //首页
        $sitemap .='<url>';
        $sitemap .='<loc>'.getDomain().'</loc>';
        $sitemap .='<lastmod>'.date('Y-m-d',time()).'</lastmod>';
        $sitemap .='<changefreq>daily</changefreq>';
        $sitemap .='<priority>0.9</priority>';
        $sitemap .='</url>';

        $sitemap_tt .='<url>';
        $sitemap_tt .='<loc>'.getDomain().'</loc>';
        $sitemap_tt .='<lastmod>'.date('Y-m-d',time()).'</lastmod>';
        $sitemap_tt .='</url>';

        $sitemap_tt_m .='<url>';
        $sitemap_tt_m .='<loc>'.getMDomain().'</loc>';
        $sitemap_tt_m .='<lastmod>'.date('Y-m-d',time()).'</lastmod>';
        $sitemap_tt_m .='</url>';



        //列表页
        $typeList = Config::get("type");
        if(!empty($typeList)){
            foreach($typeList as $k=>$v){
                $sitemap .='<url>';
                $sitemap .='<loc>'.$v['url'].'</loc>';
                $sitemap .='<lastmod>'.date('Y-m-d',time()).'</lastmod>';
                $sitemap .='<changefreq>daily</changefreq>';
                $sitemap .='<priority>0.7</priority>';
                $sitemap .='</url>';

                $sitemap_tt .='<url>';
                $sitemap_tt .='<loc>'.$v['url'].'</loc>';
                $sitemap_tt .='<lastmod>'.date('Y-m-d',time()).'</lastmod>';
                $sitemap_tt .='</url>';

                $sitemap_tt_m .='<url>';
                $sitemap_tt_m .='<loc>'.$v['m_url'].'</loc>';
                $sitemap_tt_m .='<lastmod>'.date('Y-m-d',time()).'</lastmod>';
                $sitemap_tt_m .='</url>';


                if(!empty($v['son'])){
                    foreach($v['son'] as $key=>$val){
                        $sitemap .='<url>';
                        $sitemap .='<loc>'.$val['url'].'</loc>';
                        $sitemap .='<lastmod>'.date('Y-m-d',time()).'</lastmod>';
                        $sitemap .='<changefreq>daily</changefreq>';
                        $sitemap .='<priority>0.6</priority>';
                        $sitemap .='</url>';

                        $sitemap_tt .='<url>';
                        $sitemap_tt .='<loc>'.$val['url'].'</loc>';
                        $sitemap_tt .='<lastmod>'.date('Y-m-d',time()).'</lastmod>';
                        $sitemap_tt .='</url>';

                        $sitemap_tt_m .='<url>';
                        $sitemap_tt_m .='<loc>'.$val['m_url'].'</loc>';
                        $sitemap_tt_m .='<lastmod>'.date('Y-m-d',time()).'</lastmod>';
                        $sitemap_tt_m .='</url>';

                    }
                }
            }
        }
        return [$sitemap,$sitemap_tt,$sitemap_tt_m];
    }

    private function create_sitemap($content,$siteName){
        $sitemap ='<?xml version="1.0" encoding="UTF-8"?>';
        $sitemap .='<urlset>';
        $sitemap .=$content;
        $sitemap .='</urlset>';
        if(!empty($sitemap) && !empty($siteName)){
            $filename = $this->rootPath.$siteName.".xml";
            $xmlRs = file_put_contents($filename,$sitemap);
            if($xmlRs != false){
                echo "sitemap文件生成，路径:".$filename."\r\n";
            }
        }
    }


    private function create_sitemap_gg($content,$siteName){
        $sitemap ='<?xml version="1.0" encoding="UTF-8"?>';
        $sitemap .='<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">';
        $sitemap .=$content;
        $sitemap .='</urlset>';
        if(!empty($sitemap) && !empty($siteName)){
            $filename = $this->rootPath.$siteName.".xml";
            $xmlRs = file_put_contents($filename,$sitemap);
            if($xmlRs != false){
                echo "sitemap文件生成，路径:".$filename."\r\n";
            }
        }
    }




}